---
{
	"title": "Data Picture",
	"language": "en",
	"category": "Plugins",
	"categoryfile": "plugins",
	"description": "Allow a web page to specify different image sources to display based on media queries. The component is an event driven port of Picturefill.",
	"altLangPrefix": "data-picture",
	"dateModified": "2014-08-04"
}
---
<span class="wb-prettify all-pre hide"></span>

<section>
	<h2>Purpose</h2>
	<p>Allow a web page to specify different image sources to display based on media queries. The component is an event driven port of <a href="https://github.com/scottjehl/picturefill">Picturefill</a>.</p>
</section>

<section>
	<h2>Use when</h2>
	<ul>
		<li>Displaying different sized images for different browser screen sizes.</li>
		<li>Displaying different resolution images to devices with different pixel densities.</li>
	</ul>
</section>

<section>
	<h2>Working example</h2>
	<p><a href="../../../demos/data-picture/data-picture-en.html">English example</a></p>
	<p><a href="../../../demos/data-picture/data-picture-fr.html">French example</a></p>
</section>

<section>
	<h2>How to implement</h2>
	<ol>
		<li>Add a <code>data-pic</code> element for each image that will have multiple sources:
			<pre><code>&lt;span data-pic="data-pic" data-alt="Alt tag for the image"&gt;</code></pre>
		</li>
		<li>Nested inside the <code>data-pic</code> element, specify the default image, different images sources and fallback image:
			<ol>
				<li><strong>Default image:</strong> image that will be displayed if no media queries match or the browser doesn't support media queries:
					<pre><code>&lt;span data-src="img/default.jpg"&gt;&lt;/span&gt;</code></pre>
				</li>
				<li><strong>Different image sources:</strong> images that will be displayed if their media query matches:
					<pre><code>&lt;span data-src="img/small.jpg" data-media="(min-width: 0px)"&gt;&lt;/span&gt;
&lt;span data-src="img/medium.jpg" data-media="(min-width: 500px)"&gt;&lt;/span&gt;
&lt;span data-src="img/large.jpg" data-media="(min-width: 960px)"&gt;&lt;/span&gt;</code></pre>
				</li>
				<li><strong>Fallback image:</strong> image to display if JavaScript is not supported:
					<pre><code>&lt;noscript&gt;&lt;img src="img/fallback.jpg" alt="Alt tag for the image"/&gt;&lt;/noscript&gt;</code></pre>
				</li>
			</ol>
		</li>
	</ol>

	<p>When you're finished, you should have an element that looks like the following:</p>
	<pre><code>&lt;span data-pic="data-pic" data-alt="Alt tag for the image"&gt;
	&lt;!-- Default image --&gt;
	&lt;span data-src="img/default.jpg"&gt;&lt;/span&gt;

	&lt;!-- Images for browsers with CSS media query support --&gt;
	&lt;span data-src="img/small.jpg" data-media="(min-width: 0px)"&gt;&lt;/span&gt;
	&lt;span data-src="img/medium.jpg" data-media="(min-width: 500px)"&gt;&lt;/span&gt;
	&lt;span data-src="img/large.jpg" data-media="(min-width: 960px)"&gt;&lt;/span&gt;

	&lt;!-- Fallback content for non-JS browsers. --&gt;
	&lt;noscript&gt;&lt;img src="img/fallback.jpg" alt="Alt tag for the image"/&gt;&lt;/noscript&gt;
&lt;/span&gt;</code></pre>

</section>

<section>
	<h2>Configuration options</h2>
	<p>All configuration options of the plugin are controlled with data attributes:</p>
	<table class="table">
		<thead>
			<tr>
				<th>Option</th>
				<th>Description</th>
				<th>How to configure</th>
				<th>Values</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><code>data-pic</code></td>
				<td>Identifies this as an element that uses the data picture plugin.</td>
				<td>n/a</td>
				<td>
					<dl>
						<dt>data-picture</dt>
						<dd>The only supported value for the attribute.</dd>
					</dl>
				</td>
			</tr>
			<tr>
				<td><code>data-alt</code></td>
				<td>Provides an alt attribute value to the image element created by the plugin.</td>
				<td>Set to the string value to use as the alt attribute.</td>
				<td>
					<dl>
						<dt>None (default):</dt>
						<dd>The image element created by the plugin will not have an alt attribute.</dd>
						<dt>String:</dt>
						<dd>The string will be used as the image element's alt attribute.</dd>
					</dl>
				</td>
			</tr>
			<tr>
				<td><code>data-class</code></td>
				<td>Provides a class attribute value to the image element created by the plugin.</td>
				<td>Set to the string value to use as the image's CSS attribute.</td>
				<td>
					<dl>
						<dt>None (default):</dt>
						<dd>The image element created by the plugin will not have a class attribute.</dd>
						<dt>String:</dt>
						<dd>The string will be used as the image element's class attribute.</dd>
					</dl>
				</td>
			</tr>
			<tr>
				<td><code>data-src</code></td>
				<td>Image source that will be used if this element's media query (specified by data-media) matches.</td>
				<td>Set to the image resource path that should be displayed.</td>
				<td>
					<dl>
						<dt>None (default):</dt>
						<dd>No image will be displayed if the media query matches.</dd>
						<dt>Image path:</dt>
						<dd>The image that will be displayed if the element's media query (specified by data-media) matches.</dd>
					</dl>
				</td>
			</tr>
			<tr>
				<td><code>data-media</code></td>
				<td>Media query that determines when a given image source should be displayed.</td>
				<td>Set to the media query that should cause this element's image (specified by data-src) to display.</td>
				<td>
					<dl>
						<dt>None (default):</dt>
						<dd>The element is considered the default image and will be displayed if no media queries match or the browser does not support media queries. There can only be one default image source per data-pic element.</dd>
						<dt>Media query:</dt>
						<dd>The media query to match and determine if the element's image (specified by data-src) should be displayed.</dd>
					</dl>
				</td>
			</tr>
		</tbody>
	</table>
</section>

<section>
	<h2>Events</h2>
	<p>Document the public events that can be used by implementers or developers.</p>
	<table class="table">
		<thead>
			<tr>
				<th>Event</th>
				<th>Trigger</th>
				<th>What it does</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><code>wb-init.wb-pic</code></td>
				<td>Triggered manually (e.g., <code>$( "[data-pic]" ).trigger( "wb-init.wb-pic" );</code>).</td>
				<td>Initializes the plugin and determines which data-pic image source should be displayed. <strong>Note:</strong> the data picture plugin will be initialized automatically unless it is added after the page has already loaded.</td>
			</tr>
			<tr>
				<td><code>wb-ready.wb-pic</code> (v4.0.5+)</td>
				<td>Triggered automatically after Date Picture initializes.</td>
				<td>Used to identify where Data Picture was initialized (target of the event)
					<pre><code>$( document ).on( "wb-ready.wb-pic", function( event ) {
});</code></pre>
					<pre><code>$( "[data-pic]" ).on( "wb-ready.wb-pic", function( event ) {
});</code></pre>
				</td>
			</tr>
			<tr>
				<td><code>wb-updated.wb-pic</code> (v4.0.5+)</td>
				<td>Triggered automatically each time the picture is updated.</td>
				<td>Used to identify which picture was updated (target of the event)
					<pre><code>$( document ).on( "wb-updated.wb-pic", "[data-pic]", function( event ) {
});</code></pre>
					<pre><code>$( "[data-pic]" ).on( "wb-updated.wb-pic", function( event ) {
});</code></pre>
				</td>
			</tr>
			<tr>
				<td><code>picfill.wb-pic</code></td>
				<td>Triggered during plugin initialization and manually (e.g., <code>$( "[data-pic]" ).trigger( "picfill.wb-pic" );</code>).</td>
				<td>Causes the data-pic element to determine which image source should be displayed. This could be called as part of a window resize event handler.</td>
			</tr>
			<tr>
				<td><code>wb-ready.wb</code> (v4.0.5+)</td>
				<td>Triggered automatically when WET has finished loading and executing.</td>
				<td>Used to identify when all WET plugins and polyfills have finished loading and executing.
					<pre><code>$( document ).on( "wb-ready.wb", function( event ) {
});</code></pre>
				</td>
			</tr>
		</tbody>
	</table>
</section>

<section>
	<h2>Source code</h2>
	<p><a href="https://github.com/wet-boew/wet-boew/tree/master/src/plugins/data-picture">Data picture source code on GitHub</a></p>
</section>
